package bean;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;

public class Ticket {
    //存放已发售的彩票的容器S1，用于比对后续产生的彩票是否重复
    static ArrayList<String> S1 = new ArrayList(300);
    //存放当前可购买的彩票的容器S2
    static ArrayList<String> S2 = new ArrayList(50);
    //存放线程一已购彩票的容器A1
    static ArrayList<String> A1 = new ArrayList(60);
    //存放线程二已购彩票的容器A2
    static ArrayList<String> A2 = new ArrayList(60);
    //存放线程三已购彩票的容器A3
    static ArrayList<String> A3 = new ArrayList(60);
    //存放线程四已购彩票的容器A4
    static ArrayList<String> A4 = new ArrayList(60);

    static Random random = new Random();
    //定义发售的彩票数量
    static final int MAX = 20;
    //定义中奖号码
    static final String LUCK_NUM = "18";
    private int num;
    private static boolean flag = true;
    //生产彩票
    public synchronized void produceTicket(){
        String s;
        //判断S2容器是否已满
        if (flag) {
            //产生彩票号码
            S:while (true) {
                s = String.valueOf(random.nextInt(MAX) + 1);//生成1~100的彩票号码
                //在容器S1中比对该彩票号码是否已经生产过
                if (!NumIsExist(s)) {
                    S1.add(s);
                    S2.add(s);
                    System.out.println("生产了彩票号码："+s);
                    break S;
                }
            }
            flag = false;
            this.notifyAll();
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //购买彩票
    public synchronized void getTicket(){
        int i=0;
        String s;
        String name;
        String luckThread=" ";
        name = Thread.currentThread().getName();
        if (!flag){
            //获取可售彩票容器（S2）中下标为0的彩票
            s = get(i);
            if (name.equals("Thread-1")){
                //被线程一抢到买彩票任务，所以将该彩票存入A1中
                A1.add(s);
                System.out.println("线程一购买了彩票号码:"+s);
            }if (name.equals("Thread-2")){
                //被线程二抢到买彩票任务，所以将该彩票存入A2中
                A2.add(s);
                System.out.println("线程二购买了彩票号码:"+s);
            }if (name.equals("Thread-3")){
                //被线程三抢到买彩票任务，所以将该彩票存入A3中
                A3.add(s);
                System.out.println("线程三购买了彩票号码:"+s);
            }if (name.equals("Thread-4")){
                //被线程四抢到买彩票任务，所以将该彩票存入A4中
                A4.add(s);
                System.out.println("线程四购买了彩票号码:"+s);
            }
            if (S1.size()==MAX){
                //彩票已全部发售,公布结果
                System.out.println("\n全部彩票发售完成,中奖号码是："+LUCK_NUM);
                System.out.println("线程一购买到的彩票号码为：");
                for (String S :A1){
                    if (S.equals(LUCK_NUM)){
                        luckThread = "线程一";
                    }
                    System.out.print(S+" ");
                }
                System.out.println("\n线程二购买到的彩票号码为：");
                for (String S2 :A2){
                    if (S2.equals(LUCK_NUM)){
                        luckThread = "线程二";
                    }
                    System.out.print(S2+" ");
                }
                System.out.println("\n线程三购买到的彩票号码为：");
                for (String S3 :A3){
                    if (S3.equals(LUCK_NUM)){
                        luckThread = "线程三";
                    }
                    System.out.print(S3+" ");
                }
                System.out.println("\n线程四购买到的彩票号码为：");
                for (String S4 :A4){
                    if (S4.equals(LUCK_NUM)){
                        luckThread = "线程四";
                    }
                    System.out.print(S4+" ");
                }
                System.out.println("\n中奖的线程为："+luckThread);
                System.exit(1);
            }
            flag = true;
            this.notifyAll();
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static String get(int i){
        //将该彩票号码从可售彩票容器中删除
        return S2.remove(i);
    }
    //判断彩票是否已生产
    public static boolean NumIsExist(String s){
        //判断是否是生产的第一张彩票号码，如果是，不用比对直接返回false
        if (S1.size() == 0){
            return false;
        }
        //将该彩票号码与已生产的彩票号码比对，判断是否重复
        for (int i = 0;i<S1.size();i++){
            if (Objects.equals(s, S1.get(i))){
                return true;
            }
        }
        return false;
    }
}
